package db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import db.Controller;
import db.dto.LanguagesDTO;

public class LanguagesDAO {
	public LanguagesDTO getLanguage(int id) {
		LanguagesDTO lan = null;
		String query = "select * from m_languages where m_id = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				lan = new LanguagesDTO();
				lan.setId(rs.getInt("m_id"));
				lan.setName(rs.getString("m_name"));
			}
		} catch (SQLException e) {
			return null;
		}
		return lan;
	}
	public boolean deleteLanguage(int id) {
		String query = "delete from m_languages where m_id = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	public boolean addLanguage(String name) {
		String query = "insert into m_languages(m_name) values(?)";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setString(1, name);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
}
